﻿using M_Process.API.Appliction.Command.ProcessRouteCommand;
using M_Process.Domain;
using M_Process.Repository.ProcessInterface;
using MediatR;
using Microsoft.AspNetCore.Http.HttpResults;

namespace M_Process.API.Appliction.CommandHandlers.ProcessRouteHandler
{
    /// <summary>
    /// 新增工艺信息
    /// </summary>
    public class ProcessRouteCreateCommandHabdler : IRequestHandler<ProcessRouteCreateCommand, int>
    {
        /// <summary>
        /// 工艺仓储接口
        /// </summary>
        private readonly IProcessRouteRepository _processRouteRepository;
        /// <summary>
        /// 工艺工序关联表仓储接口
        /// </summary>
        protected readonly IProcessProceduresRepository _ProceduresRepository;
        public ProcessRouteCreateCommandHabdler(IProcessRouteRepository processRouteRepository, IProcessProceduresRepository ProceduresRepository)
        {
            _processRouteRepository = processRouteRepository;
            _ProceduresRepository = ProceduresRepository;
        }
        public async Task<int> Handle(ProcessRouteCreateCommand request, CancellationToken cancellationToken)
        {
           int id= await _processRouteRepository.AddAsyncs(new ProcessRoute()
            {
                
                ProcessPouteNumber = request.ProcessPouteNumber,
                ProcessRouteName = request.ProcessRouteName,
                State = request.State,
                ProductName = request.ProductName,
               CreatedBy=request.CreatedBy
           });
            foreach (var item in request.ProductionProcessesId)
            {
                await _ProceduresRepository.AddAsync(new ProcessProcedures()
                {         QualityprogramId=request.QualityprogramId,//质检组ID
                          ProcessRouteId=id,//工艺ID
                    ProductionProcessesId = item//工序ID

                });
            }
           
            return 1;
        }
    }
}
